package org.jsola.contract.web;

import org.jsola.contract.constant.ContractConstants;
import org.jsola.contract.dto.CompanyContractShowAddDTO;
import org.jsola.contract.dto.CompanyContractShowUpdateDTO;
import org.jsola.contract.query.CompanyContractShowQuery;
import org.jsola.contract.service.ICompanyContractShowService;
import org.jsola.contract.vo.CompanyContractShowListVO;
import org.jsola.contract.vo.CompanyContractShowVO;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 合同员工签署后是否展示
 *
 * @author JanusMix
 */
@RestController("contractCompanyContractShowController")
@RequestMapping(ContractConstants.API_PATH + "/v1/companyContractShows")
public class CompanyContractShowController implements ICompanyContractShowControllerAPI {

    @Autowired
    private ICompanyContractShowService companyContractShowService;

    @Override
    @PostMapping
    public Result<CompanyContractShowVO> save(@Valid @RequestBody CompanyContractShowAddDTO companyContractShowAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增合同员工签署后是否展示权限校验

        return Result.success(
                companyContractShowService.save(companyContractShowAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody CompanyContractShowUpdateDTO companyContractShowUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改合同员工签署后是否展示权限校验

        int count = companyContractShowService.update(companyContractShowUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{companyContractShowId}")
    public Result<Integer> delete(@PathVariable String companyContractShowId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除合同员工签署后是否展示权限校验

        int count = companyContractShowService.deleteByIds(tokenUser, companyContractShowId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] companyContractShowIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除合同员工签署后是否展示权限校验

        int count = companyContractShowService.deleteByIds(tokenUser, companyContractShowIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{companyContractShowId}")
    public Result<CompanyContractShowVO> findById(@PathVariable String companyContractShowId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看合同员工签署后是否展示权限校验

        return Result.success(
            companyContractShowService.selectById(companyContractShowId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<CompanyContractShowListVO>> listAll(@Valid CompanyContractShowQuery companyContractShowQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看合同员工签署后是否展示权限校验

        return Result.success(
            companyContractShowService.select(companyContractShowQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<CompanyContractShowListVO>> page(@Valid CompanyContractShowQuery companyContractShowQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            companyContractShowService.selectPage(companyContractShowQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/findByCompanyId/{companyId}")
    public Result<CompanyContractShowVO> findByCompanyId(@PathVariable String companyId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                companyContractShowService.findByCompanyId(companyId, tokenUser)
        );
    }

    @Override
    @PutMapping(value = "/updateCompanyShow/{companyId}/{isShow}")
    public Result<String> updateCompanyShow(@PathVariable String companyId,
                                            @PathVariable Boolean isShow) {

        TokenUser tokenUser = UserContext.getUser();

        companyContractShowService.updateCompanyShow(companyId, isShow, tokenUser);
        return Result.success("修改成功");
    }


}

